package svc

import (
	"github.com/go-redis/redis/v8"
	"go-zero-project/services/user/rpc/internal/config"
	"go-zero-project/services/user/rpc/internal/initialize/mysql"
	redis2 "go-zero-project/services/user/rpc/internal/initialize/redis"
	"gorm.io/gorm"
)

type ServiceContext struct {
	Config      config.Config
	DBEngin     *gorm.DB
	RedisClient *redis.Client
}

func NewServiceContext(c config.Config) *ServiceContext {
	// init DB ..
	db, dbErr := mysql.InitMysqlDB(c.Mysql)
	s := &ServiceContext{}
	s.Config = c
	if dbErr != nil {
		return s
	}
	// init Redis ..
	initRedis, redisErr := redis2.InitRedis(c.Redis)
	if redisErr != nil {
		s.DBEngin = db
		return s
	}
	return &ServiceContext{
		Config:      c,
		DBEngin:     db,
		RedisClient: initRedis,
	}
}
